package com.hngates.project.controller;

import java.util.List;

import com.hngates.common.utils.ShiroUtils;
import com.hngates.project.domain.ConstructionUnit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hngates.common.annotation.Log;
import com.hngates.common.enums.BusinessType;
import com.hngates.project.domain.LaborSubcontractor;
import com.hngates.project.service.ILaborSubcontractorService;
import com.hngates.common.core.controller.BaseController;
import com.hngates.common.core.domain.AjaxResult;
import com.hngates.common.utils.poi.ExcelUtil;
import com.hngates.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 劳务分包单位Controller
 * 
 * @author ruoyi
 * @date 2022-06-24
 */
@Controller
@RequestMapping("/project/laborSubcontractor")
public class LaborSubcontractorController extends BaseController
{
    private String prefix = "project/laborSubcontractor";

    @Autowired
    private ILaborSubcontractorService laborSubcontractorService;

    @RequiresPermissions("project:laborSubcontractor:view")
    @GetMapping()
    public String laborSubcontractor()
    {
        return prefix + "/laborSubcontractor";
    }

    /**
     * 查询劳务分包单位列表
     */
    @RequiresPermissions("project:laborSubcontractor:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LaborSubcontractor laborSubcontractor)
    {
        startPage();
        List<LaborSubcontractor> list = laborSubcontractorService.selectLaborSubcontractorList(laborSubcontractor);
        return getDataTable(list);
    }

    /**
     * 导出劳务分包单位列表
     */
    @RequiresPermissions("project:laborSubcontractor:export")
    @Log(title = "劳务分包单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LaborSubcontractor laborSubcontractor)
    {
        List<LaborSubcontractor> list = laborSubcontractorService.selectLaborSubcontractorList(laborSubcontractor);
        ExcelUtil<LaborSubcontractor> util = new ExcelUtil<LaborSubcontractor>(LaborSubcontractor.class);
        return util.exportExcel(list, "劳务分包单位数据");
    }

    /**
     * 下载模板
     *
     * @return
     */
    @RequiresPermissions("project:laborSubcontractor:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<LaborSubcontractor> util = new ExcelUtil<>(LaborSubcontractor.class);
        return util.importTemplateExcel("劳务分包单位数据");
    }

    /**
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @RequiresPermissions("project:laborSubcontractor:import")
    @PostMapping("/importLaborSubcontractor")
    @ResponseBody
    public AjaxResult importLaborSubcontractor(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<LaborSubcontractor> util = new ExcelUtil<>(LaborSubcontractor.class);
        List<LaborSubcontractor> laborSubcontractorList = util.importExcel(file.getInputStream(), 1);
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = laborSubcontractorService.importLaborSubcontractor(laborSubcontractorList, updateSupport, operName);
        return AjaxResult.success(message);
    }



    /**
     * 新增劳务分包单位
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存劳务分包单位
     */
    @RequiresPermissions("project:laborSubcontractor:add")
    @Log(title = "劳务分包单位", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LaborSubcontractor laborSubcontractor)
    {
        return toAjax(laborSubcontractorService.insertLaborSubcontractor(laborSubcontractor));
    }

    /**
     * 修改劳务分包单位
     */
    @RequiresPermissions("project:laborSubcontractor:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        LaborSubcontractor laborSubcontractor = laborSubcontractorService.selectLaborSubcontractorById(id);
        mmap.put("laborSubcontractor", laborSubcontractor);
        return prefix + "/edit";
    }

    /**
     * 修改保存劳务分包单位
     */
    @RequiresPermissions("project:laborSubcontractor:edit")
    @Log(title = "劳务分包单位", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LaborSubcontractor laborSubcontractor)
    {
        return toAjax(laborSubcontractorService.updateLaborSubcontractor(laborSubcontractor));
    }

    /**
     * 删除劳务分包单位
     */
    @RequiresPermissions("project:laborSubcontractor:remove")
    @Log(title = "劳务分包单位", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(laborSubcontractorService.deleteLaborSubcontractorByIds(ids));
    }
}
